<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * @ID model_lable.php
 * 地标model
 * @date 2013-2-20
 * @author yuhailong zjj008@gmail.com
 * @copyright zhuna Inc , all rights reserved
 * 
 */

class Model_lable extends CI_Model
{
	private $cachePath;
	
	function __construct()
    {
        parent::__construct();
        
        $this->cachePath = ROOTPATH.DIRECTORY_SEPARATOR.$this->config->config['cache_path'];//缓存存放目录
    }
    
    /**
     * 首页ajax获得地标信息
     * @param int $cityid  城市id
     * @param int $type  地标类型
     * @return array $lableInfo 地标数组
     */
    public function getLable($cityid,$type,$rows)
    {
    	$cityArr = $this->model_city->get_local_city_byid($cityid);
    	$areaid = $cityArr['areaid'];
    	$inserfaceUrl = CFG_INTERFACE_API."lable&cityid={$areaid}&classid={$type}&rows={$rows}";
    	$cacheName = 'lable_'.$cityid.'_'.$type.'.json';
    	$lableJSON = $this->tool->create_cache($inserfaceUrl,$this->cachePath."lable/{$cityid}/",$cacheName);
    	$lableJSON = json_decode($lableJSON,true);
    	return $lableJSON = json_encode($lableJSON['reqdata']);
    }
    
    /**
     * 首页ajax获得地标信息：地铁
     * @param int $cityid  城市id
     * @param int $type  地标类型
     * @return array $lableInfo 地标数组
     */
    public function getSubwayLable($cityid)
    {
    	$rs = "" ;
    	$cacheName = 'lable_'.$cityid.'_subway.json';
    	$cachepath = $this->cachePath."lable/{$cityid}/";
    	$cache = $cachepath . $cacheName;  //xml接口的本地路径+文件全名
    	$checktime = 259200;
    	if ($this->tool->check_cache($cache, $checktime)) {
    		$this->tool->CreateFolder($cachepath);
    		
    		$getLineUrl = CFG_INTERFACE_API."subway.line&cityid=$cityid";
    		$subwaylineJSON = file_get_contents($getLineUrl);
    		$subwayline = json_decode($subwaylineJSON,true);
    		$subwayline = $subwayline['reqdata'];
    		
    		$subwayUrl = CFG_INTERFACE_API."subway.station&lineid=";
    		$rs = array();
    		foreach ($subwayline as $key=>$val) {
    			$subwayJSON = file_get_contents($subwayUrl.$val['zhuantiid']);
    			$subway = json_decode($subwayJSON,true);
    			$subway = $subway['reqdata'];
    			$rs[$val['title']] = $subway;
    		}
    		
    		$content = json_encode($rs);
    		if (strlen($content) > 0) {
    			file_put_contents($cache, $content);
    		}
    		$rs = $content;
    	}else{
    		$rs = file_get_contents($cache);
    	}
    	
    	return $rs;
    }
}
?>